@extends('admin.layout.base')
@section('styles')
@endsection
@section('content')
    <div class="panel-content" id="app" v-cloak>
        <div class="panel-body">
            <div class="panel-header">
                <div class="panel-title"> 供应商列表 </div>
                <el-tabs v-model="tabValue" type="card" @tab-click="search()">
                    <el-tab-pane label="全 部" name="0"></el-tab-pane>
                    <el-tab-pane label="正 常" name="1"></el-tab-pane>
                    <el-tab-pane label="待审核" name="2"></el-tab-pane>
                    <el-tab-pane label="未通过" name="3"></el-tab-pane>
                    <el-tab-pane label="锁 定" name="4"></el-tab-pane>
                    <el-tab-pane label="已欠费" name="5"></el-tab-pane>
                </el-tabs>
            </div>
            <div class="panel-main">
                <div class="panel-btns">
                    <el-button type="success" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                    <el-button type="primary" icon="fa fa-plus" @click="exports()" size="small"> 导出</el-button>
                    {{--<el-button type="info" icon="fa fa-trash" @click="multiDelete()" size="small"> 删除</el-button>--}}
                </div>
                <div class="panel-search">
                    <div class="panel-search-item">
                        <el-input  size="medium" v-model="params.kwd" clearable placeholder="请输入手机账号或者公司名称" class="input-with-select">
                            <el-button type="primary" @click="search()" slot="append" icon="el-icon-search"></el-button>
                        </el-input>
                    </div>
                </div>
                <el-table v-loading="loading"
                          element-loading-text="努力加载中..."
                          element-loading-spinner="el-icon-loading"
                          ref="multipleHandle"
                          tooltip-effect="dark"
                          @selection-change="handleSelectionChange"
                          :data="listData.data" border>
                    <el-table-column align="center" type="selection" width="40"></el-table-column>
                    <el-table-column align="center" prop="avatar" label="头像" width="85">
                        <template slot-scope="scope"> <img :src="scope.row.avatar" style="height: 60px;width: 60px;border-radius: 50%"/> </template>
                    </el-table-column>
                    <el-table-column align="center" prop="user_info.company_name" label="公司名" width="120" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column align="center" prop="user_info.master_name" label="联系人" width="80"></el-table-column>
                    <el-table-column align="center" prop="sex_text" label="性别" width="60">
                        <template slot-scope="scope"> <span v-html="scope.row.sex_text"></span> </template>
                    </el-table-column>
                    <el-table-column align="center" prop="mobile" label="手机" width="120"></el-table-column>
                    <el-table-column align="center" prop="wechat" label="微信" width="120"></el-table-column>
                    <el-table-column align="center" prop="qq" label="QQ" width="120"></el-table-column>
                    <el-table-column align="center" prop="user_info.company_email" label="邮箱" width="120" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column align="center" prop="balance" label="余额" width="90"></el-table-column>
                    <el-table-column align="center" prop="integral" label="积分" width="80"></el-table-column>
                    <el-table-column align="center" prop="status_text" label="状态" width="80">
                        <template slot-scope="scope"> <span v-html="scope.row.status_text"></span> </template>
                    </el-table-column>
                    <el-table-column align="center" prop="login_at" label="最近登录" width="165"></el-table-column>
                    <el-table-column align="center" label="操作" >
                        <template slot-scope="scope">
                            <el-button v-if="scope.row.status == 4" @click="active(scope.row.id)" icon="fa fa-unlock-alt" type="success" size="mini" plain>启用</el-button>
                            <el-button v-if="scope.row.status == 1" @click="close(scope.row.id)" icon="fa fa-lock" type="danger" size="mini" plain>锁定</el-button>
                            <el-button @click="edit(scope.row.id)" icon="fa fa-edit" type="primary" size="mini" plain>详情</el-button>
                            {{--<el-button @click="destroy(scope.row.id)" icon="fa fa-trash" type="info" size="mini" plain> 删除</el-button>--}}
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination"  v-if="listData && listData.total">
                    <div class="pagination-total">共 <em> @{{listData.total}} </em> 条记录</div>
                    <el-pagination v-if="listData.last_page > 1"
                                   background
                                   :page-size="listData.per_page"
                                   layout="prev, pager, next, jumper"
                                   :total="listData.total"
                                   prev-text="上一页"
                                   next-text="下一页"
                                   :current-page="listData.current_page"
                                   @current-change="changePage">
                    </el-pagination>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('scripts')
    <script>
        let app = new Vue({
            el: '#app',
            data: function() {
                return {
                    listData: [],//列表数据
                    tabValue: 0,//tab
                    params: {page: 1, kwd: '', status: 0, type: 2},
                    loading: false,
                    multipleSelection:[],//多选项
                    form:null,
                }
            },
            created(){
                this.list();
            },
            methods: {
                //分页
                changePage(val) {
                    this.params.page = val;
                    this.list();
                },
                //列表
                list(){
                    let that = this;
                    this.loading = true;
                    setTimeout(()=>{
                        $.get("{{ route('admin.user.list') }}",this.params,function (res) {
                            if(res.code){
                                that.$message.error(res.msg);
                            }else{
                                that.listData = res.data;
                                that.loading = false;
                            }
                        },'JSON');
                    },500);
                },
                //编辑
                edit(id){
                    window.location.href = "{{ route('admin.user.supplierEdit') }}" + '?id=' + id;
                },
                //导出
                exports(){
                    if(this.multipleSelection.length == 0){
                        var msg = '确定要导出所有的用户信息吗？';
                    }else {
                        var msg = '确定要导出选择的用户信息吗？';
                    }
                    this.$confirm(msg, '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        window.location.href = "{{ route('admin.user.supplierExports') }}"+'?ids='+this.multipleSelection.join('-');
                    }).catch(() => {});
                },
                //筛选
                search(){
                    this.params.page = 1;
                    switch (this.tabValue) {
                        case '1':
                            this.params.status = 1;
                            this.params.balance = '';
                            break;
                        case '2':
                            this.params.status = 2;
                            this.params.balance = '';
                            break;
                        case '3':
                            this.params.status = 3;
                            this.params.balance = '';
                            break;
                        case '4':
                            this.params.status = 4;
                            this.params.balance = '';
                            break;
                        case '5':
                            this.params.balance = true;
                            this.params.status = 0;
                            break;
                        default:
                            this.params.status = 0;
                            this.params.balance = '';
                            break;
                    }
                    this.list();
                },
                //拒绝
                lock(id){
                    let that = this;
                    $.post("{{ route('admin.user.lock') }}",{id: id, _token: "{{csrf_token()}}"},function (res) {
                        if(res.code){
                            that.$message.error(res.msg);
                        }else{
                            that.list();
                        }
                    },'JSON');
                },
                //激活
                active(id){
                    let that = this;
                    $.post("{{ route('admin.user.active') }}",{id: id, _token: "{{csrf_token()}}"},function (res) {
                        if(res.code){
                            that.$message.error(res.msg);
                        }else{
                            that.list();
                        }
                    },'JSON');
                },
                //锁定
                close(id){

                    this.$prompt('请输入锁定原因', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        inputErrorMessage: '请输入锁定原因'
                    }).then(({ value }) => {
                        let that = this;
                        $.post("{{ route('admin.user.close') }}",{id: id,reason: value, _token: "{{csrf_token()}}"},function (res) {
                            if(res.code){
                                that.$message.error(res.msg);
                            }else{
                                that.list();
                            }
                        },'JSON');
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '取消输入'
                        });
                    });
                },
                // 多选删除
                multiDelete(){
                    if(this.multipleSelection.length == 0){
                        this.$message.error('请至少选择一项你要删除的项');return false;
                    }
                    this.destroy(this.multipleSelection);
                },
                handleSelectionChange(val) {
                    let select = [];
                    if(val.length > 0){
                        val.filter((item)=>{
                            if(select.indexOf(item.id) === -1){
                                select.push(item.id)
                            }
                        });
                        this.multipleSelection = select;
                    }else{
                        this.multipleSelection = [];
                    }
                },
                //删除
                destroy(id){
                    let that = this;
                    this.$confirm('确定要删除选择的用户信息吗？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        $.post("{{ route('admin.user.destroy') }}",{id: id, _token: "{{csrf_token()}}"},function (res) {
                            if(res.code){
                                that.$message.error(res.msg);
                            }else{
                                that.list();
                            }
                        },'JSON');
                    }).catch(() => {});
                },
            }
        });
    </script>
@endsection
